package com.caifu.controller;

import com.caifu.bean.Result;
import com.caifu.pojo.CUser;
import com.caifu.pojo.SysUser;
import com.caifu.service.DeptService;
import com.caifu.service.UserService;
import com.caifu.util.BaiduFaceUtils;
import com.caifu.util.Utils;
import com.caifu.vo.UserVo;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 人员管理
 */
@Controller
@RequestMapping("cuser")
public class CCUserController extends AbstractController {

    final static Logger logger = LoggerFactory.getLogger(CCUserController.class);

    @Resource
    private UserService userService;

    @Resource
    private DeptService deptService;

    private String pre = "view/client/";

    @GetMapping("toUserPage")
    public String toUserPage() {
        return pre + "user_list";
    }

    @GetMapping("toUserDetailsPage")
    public String toUserDetailsPage(String userNo, Model model) {
        model.addAttribute("userNo", userNo);
        return pre + "user_details";
    }

    @GetMapping("toUserAddPage")
    public String toUserAddPage() {
        return pre + "user_add";
    }

    /**
     * 人员列表
     *
     * @param
     * @return
     * @throws Exception
     */
    @RequestMapping("list")
    @ResponseBody
    public Result list(UserVo userVo) throws Exception {
        SysUser user = getUser();
        try {
            return success(0, "", userService.getUserList(user, userVo));
        } catch (Exception e) {
            String msg = "网络异常";
            if (Utils.isNotNull(e.getMessage())) {
                msg = e.getMessage();
            }
            logger.error(e.toString());
            e.printStackTrace();
            return error(500, msg);
        }
    }

    /**
     * 人员新增
     *
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping("add")
    public String add(ModelMap model) throws Exception {
        SysUser user = getUser();
        try {
            return "view/user/add";
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * 人员编辑
     *
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping("edit")
    public String edit(ModelMap model, String userNo) throws Exception {
        SysUser user = getUser();
        try {
            CUser userInfo = userService.getUserInfo(userNo);
            model.put("user", userInfo);
            return "view/user/edit";
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * 人员详情
     *
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping("detail")
    public String detail(ModelMap model) throws Exception {
        SysUser user = getUser();
        try {
            return "view/user/detail";
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 人脸识别
     *
     * @return Result
     */
    @PostMapping(value = "/isFaceApiIdentify")
    @ResponseBody
    public JSONObject isFaceApiIdentify(String imageUrl) {
        logger.info("imageUrl1234=" + imageUrl);
        try {
            Map<String, Object> map = BaiduFaceUtils.isFaceApiIdentify(imageUrl);
            return JSONObject.fromObject(map);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取部门设备列表
     */
    @RequestMapping("getDeptDevTree")
    @ResponseBody
    public Result getDeptDevTree() {
        SysUser user = getUser();
        try {
            return success(0, "", deptService.getDeptDevTree(user));
        } catch (Exception e) {
            String msg = "网络异常";
            if (Utils.isNotNull(e.getMessage())) {
                msg = e.getMessage();
            }
            logger.error(e.toString());
            e.printStackTrace();
            return error(500, msg);
        }
    }

    /**
     * 获取部门设备列表
     */
    @RequestMapping("getDevByDeptNo")
    @ResponseBody
    public Result getDevByDeptNo(Integer deptNo) {
        SysUser user = getUser();
        try {
            return success(0, "", deptService.getDevByDeptNo(user, deptNo));
        } catch (Exception e) {
            String msg = "网络异常";
            if (Utils.isNotNull(e.getMessage())) {
                msg = e.getMessage();
            }
            logger.error(e.toString());
            e.printStackTrace();
            return error(500, msg);
        }
    }

    @PostMapping("addUser")
    @ResponseBody
    public Result addUser(@RequestBody CUser cUser) {
        SysUser user = getUser();
        try {
            return userService.addOrUpdate(cUser, user);
        } catch (Exception e) {
            String msg = "网络异常";
            if (Utils.isNotNull(e.getMessage())) {
                msg = e.getMessage();
            }
            logger.error(e.toString());
            e.printStackTrace();
            return error(500, msg);
        }
    }

    @GetMapping("getUserById")
    @ResponseBody
    public Result getUserById(String userNo) {
        SysUser user = getUser();
        try {
            return success(200, "", userService.getUserById(user, userNo));
        } catch (Exception e) {
            String msg = "网络异常";
            if (Utils.isNotNull(e.getMessage())) {
                msg = e.getMessage();
            }
            logger.error(e.toString());
            e.printStackTrace();
            return error(500, msg);
        }
    }


    @DeleteMapping("deleteById")
    @ResponseBody
    public Result deleteById(String userNo) {
        SysUser user = getUser();
        try {
            return userService.deleteById(userNo);
        } catch (Exception e) {
            String msg = "网络异常";
            if (Utils.isNotNull(e.getMessage())) {
                msg = e.getMessage();
            }
            logger.error(e.toString());
            e.printStackTrace();
            return error(500, msg);
        }
    }

    public static void main(String[] args) {
        String random = Utils.getRandom(4);
        System.out.println("s"+random);
    }

}
